package calao.client;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import calao.client.ui.Elements;
import calao.client.ui.FastTable;
import calao.client.ui.HasTitle;
import calao.client.ui.Link;
import calao.client.ui.SpacerPanel;
import calao.client.ui.Styles;

public class ViewPanel
extends SpacerPanel
{
	
	public static final String STYLE = "calao-ViewPanel";
	public static final String LINKS_STYLE = "calao-ViewPanel-links";
	public static final String TITLE_STYLE = "calao-ViewPanel-title";
	public static final String STATUS_STYLE = "calao-ViewPanel-status";

	final ViewPanel parent;
	final private String title;
	final Widget content;
	final FastTable links = new FastTable(2);
	
	final Link nextBackLink = new Link() {
		protected void onClick() {
			Desktop.getDesktop().popView();
		}		
	};
	
	Widget status;

	ViewPanel(Widget content, String title, ViewPanel parent) 
	{
		super(SpacerPanel.VERTICAL);
		this.title = title;
		this.content = content;
		this.parent = parent;
		addStyleName(STYLE);
		links.addStyleName(LINKS_STYLE);
		refresh();
	}
	
	public void refresh() 
	{
		if (getWidgetCount()>0) clear();
		String title = getTitle();
		if (title!=null) {
			Label label = new Label(title);
			label.addStyleName(TITLE_STYLE);
			add(label);
			nextBackLink.setText(title);
		}
		else {
			nextBackLink.setText(Desktop.getDesktop().text.previousView());
		}
		if (status!=null) add(status);
		add(content);
		add(links);
	}
	
	public void addLink(Link link, String header)
	{
		links.addCell(header, 1, Styles.HEADER);
		links.addCell(link);
	}
	
	public void addLinkSpacer()
	{
		Elements.setHeight(links.addEmptyCell(2), DEFAULT_SPACE_SIZE);
	}
	
	public int getLinkCount()
	{
		return links.getRowCount();
	}
	
	public void setStatus(Widget status)
	{
		if (status!=null) status.addStyleName(STATUS_STYLE);
		this.status = status;
	}
	
	public void clearStatus()
	{
		if (status!=null) remove(status);
	}
	
	public Widget getContent()
	{
		return content;
	}
	
	public String getTitle()
	{
		if (title!=null) return title;
		if (content instanceof HasTitle) return ((HasTitle)content).getTitle();
		return null;
	}
		
}
